package com.gitee.feizns.itrip.personal.biz.controller;

import com.gitee.feizns.itrip.personal.beans.dto.Dto;
import com.gitee.feizns.itrip.personal.beans.pojo.User;
import com.gitee.feizns.itrip.personal.beans.pojo.UserLinkUser;
import com.gitee.feizns.itrip.personal.biz.entity.ao.ForAcceptQueryLinkUser;
import com.gitee.feizns.itrip.personal.biz.entity.ao.ForAcceptUserLinkUser;
import com.gitee.feizns.itrip.personal.biz.service.UserInfoService;
import com.gitee.feizns.itrip.personal.utils.component.annotations.CurtUser;
import com.gitee.feizns.itrip.personal.utils.component.annotations.TokenValidate;
import com.gitee.feizns.itrip.personal.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author feizns
 * @since 2019/1/10 0010
 */
@TokenValidate
@RestController
@RequestMapping("/api/userinfo")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("/queryuserlinkuser")
    public Dto<List<UserLinkUser>> queryUserLinkUser(@RequestBody ForAcceptQueryLinkUser queryLinkUser, @CurtUser User curtUser) {
        return Dto.getSuccessDto("获取常用联系人信息成功", userInfoService.queryUserLinkUser(queryLinkUser, curtUser.getId()));
    }

    @PostMapping("/adduserlinkuser")
    public Dto<?> addUserLinkUser(@RequestBody @Validated ForAcceptUserLinkUser fuser, @CurtUser User curtUser) {
        UserLinkUser user = Utils.copyTo(fuser, UserLinkUser.class);
        userInfoService.addUserLinkUser(user, curtUser.getId());
        return Dto.getSuccessDto("新增常用联系人成功", null);
    }

    @PostMapping("/modifyuserlinkuser")
    public Dto<?> modifyUserLinkUser(@RequestBody @Validated ForAcceptUserLinkUser linkUser, @CurtUser User curtUser) {
        UserLinkUser user = Utils.copyTo(linkUser, UserLinkUser.class);
        userInfoService.modifyUserLinkUser(user, curtUser.getId());
        return Dto.getSuccessDto("修改常用联系人成功", null);
    }

    @GetMapping("/deluserlinkuser")
    public Dto<?> delUserLinkUser(String[] ids) {
        Integer[] newIds = Utils.parseInts(ids);
        int num = userInfoService.delUserLinkUser(newIds);
        String info = num > 1 ? "成功删除" + num + "名联系人." : "删除常用联系人成功.";
        return Dto.getSuccessDto(info, null);
    }

}
